/**
 * Icygen Create Project Wizard
 */
package com.icygen.eclipsetools.ui.wizards;


import java.net.URI;
import java.net.URISyntaxException;

import org.eclipse.jface.wizard.Wizard;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Button;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;

import com.icygen.core.svn.Repository;
import com.icygen.eclipsetools.project.IcygenModuleSupport;
import com.icygen.eclipsetools.project.IcygenProjectSupport;
import com.icygen.eclipsetools.project.config.ApplicationConfig;
import com.icygen.eclipsetools.project.config.resources.db.Params;

/**
 * @author vanko
 *
 */
public class CreateProjectWizard 
							extends Wizard 
							implements INewWizard, IExecutableExtension
{
	private IConfigurationElement _configurationElement;
	
	protected ProjectDataPage _pageProjectData;
	protected ProjectConfigPage _pageProjectConfig;
	protected ProjectModulesPage _pageProjectModules;
	protected ProjectLibrariesPage _pageProjectLibraries;
	protected ProjectThemesPage _pageProjectThemes;
	
	public CreateProjectWizard() 
	{
	    super();
	    setNeedsProgressMonitor(true);
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// TODO Auto-generated method stub
	}
	
	
	@Override
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
		_configurationElement = config;
	}

	
	@Override
	public void addPages() 
	{
		_pageProjectData = new ProjectDataPage();
		_pageProjectConfig = new ProjectConfigPage();
		_pageProjectModules = new ProjectModulesPage();
		_pageProjectLibraries = new ProjectLibrariesPage();
		_pageProjectThemes = new ProjectThemesPage();
		
	    addPage(_pageProjectData);
	    addPage(_pageProjectConfig);
	    addPage(_pageProjectModules);
	    addPage(_pageProjectLibraries);
	    addPage(_pageProjectThemes);
	}


	/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	@Override
	public boolean performFinish()
	{
		try {
			/*
			 * Create New Eclipse Project
			 */
			String projectTitle = _pageProjectData.getTitle();
		    URI projectLocation = null;
		    if (!_pageProjectData.useDefaults()) {
		    	
		    	projectLocation = _pageProjectData.getLocationURI();
		        
		    	System.err.println("location: " + projectLocation.toString()); //$NON-NLS-1$
		    } // else location == null
		 
		    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectTitle);
		    if(project.exists()) {
		    	// Има проект с такова име, изведи грешка
		    	MessageDialog.openInformation(
						this.getShell(),
						"Проектът не може да бъде създаден",
						"Има проект с такова име");
		    }
		    else {
		    	project = IcygenProjectSupport.createProject(projectTitle, projectLocation);
			    // Add this
			    BasicNewProjectResourceWizard.updatePerspective(_configurationElement);
		    }
		    
		    
		    /*
		     * Write Project Config
		     */
		    ApplicationConfig config = IcygenProjectSupport.getApplicationConfig(project);
		    Params configDbParams = config.getDevelopmentConfig().getResources().getDb().getParams();
		    
		    configDbParams.setHost(_pageProjectConfig.getHost());
		    configDbParams.setUsername(_pageProjectConfig.getUsername());
		    configDbParams.setPassword(_pageProjectConfig.getPassword());
		    configDbParams.setDbname(_pageProjectConfig.getDatabase());
		    
		    
		    config.getDevelopmentConfig().getResources().getDb().setParams(configDbParams);
		    IcygenProjectSupport.setApplicationConfig(project, config);
		    
		    
		    /*
		     * Create Project Database
		     */
		    //IcygenProjectSupport.createProjectDatabase(project);
		    
		    
		    /*
		     * Add Selected Icygen Modules into the Project
		     */
		    for(Button btnModule : _pageProjectModules.getCheckboxModules()) {
		    	if(btnModule.getSelection()) {
		    		//IcygenModuleSupport.installModule(project, btnModule.getText());
		    	}
		    }
		    
		    
		    /*
		     * Add Selected Libraries into the Project
		     */
		    for(Button btnLibrary : _pageProjectLibraries.getCheckboxLibraries()) {
		    	if(btnLibrary.getSelection()) {
		    		//Repository libraryRepository = new Repository(Repository.Libraries.get(btnLibrary.getText()), "guest", "");
		    		//libraryRepository.doExport("/", project.getLocation().append("htdocs/library/" + btnLibrary.getText()).toString());
		    	}
		    }
		    
		    
		    /*
		     * Add Selected Icygen Themes into the Project
		     */
		    Repository themesRepository = new Repository(Repository.IcygenThemesUrl, "guest", "");
		    for(Button btnTheme : _pageProjectThemes.getCheckboxThemes()) {
		    	if(btnTheme.getSelection()) {
		    		//themesRepository.doExport(btnTheme.getText(), project.getLocation().append("htdocs/public/themes/" + btnTheme.getText()).toString());
		    	}
		    }
			
			
		}
		catch(URISyntaxException e) {
			
		}
		
		return true;
	}

}
